﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weight.Base.Utility;
using Weight.Dal;
using Weight.Model;
using WeightSystem.Common;

namespace Weight.Bll
{
    public class BllEmployee
    {
        public IEnumerable<EmployeeEntity> QueryAll()
        {
            string filed = new DalField().GetSqlFieldByTabName(TableNameManager.Employee);
            return new DalEmployee().QueryAll(filed);
        }

        /// <summary>
        /// 根据账号查询
        /// </summary>
        /// <param name="account">账号</param>
        /// <returns></returns>
        public IEnumerable<EmployeeEntity>QueryByName(string account)
        {
            string filed = new DalField().GetSqlFieldByTabName(TableNameManager.Employee);
            return new DalEmployee().QueryByName(filed, account);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public EmployeeEntity Login(string account, string pwd)
        {
            pwd = new DESHelper().DesEncrypt(pwd);
            return new DalEmployee().Login(account, pwd);
        }
        /// <summary>
        /// 获取员工Id
        /// </summary>
        /// <param name="id">员工编号</param>
        /// <returns></returns>
        public EmployeeEntity GetEmpById(string id)
        {
            var entity =  new DalEmployee().GetEmpById(id);
            entity.Pwd = new DESHelper().DesDecrypt(entity.Pwd);
            return entity;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public ExeResult Add(EmployeeEntity entity)
        {
            var res = ExeResult.Defalut();
            DalEmployee dal = new DalEmployee();
            if(dal.AccountIsExist(entity.Account))
            {
                res = ExeResult.Faild("登录账号已经存在，不能重复添加！");
                return res;
            }
            else
            {
                bool b = dal.Add(entity);
                if(b)
                {
                    res = ExeResult.Success("添加成功!");
                }else
                {
                    res = ExeResult.Faild("添加失败!");
                }
            }
            return res;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public ExeResult Update(EmployeeEntity entity)
        {
            var res = ExeResult.Defalut();
            DalEmployee dal = new DalEmployee();
            if(dal.AccountIsExist(entity.Account,entity.Id))
            {
                res = ExeResult.Faild("登录账号已经存在，不能重复添加！");
                return res;
            }
            else
            {
                bool b = dal.Update(entity);
                if (b)
                {
                    res = ExeResult.Success("修改成功!");
                }
                else
                {
                    res = ExeResult.Faild("修改失败!");
                }
            }

            return res;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(string id)
        {
            return new DalEmployee().Delete(id);
        }
    }
}
